package com.lgj.springboot_lgj.web.controller;
import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.BaseUser;
import com.lgj.springboot_lgj.dao.ext.BaseUserExtMapper;
import com.lgj.springboot_lgj.service.IBaseUserService;
import com.lgj.springboot_lgj.utils.EntityUtil;
import com.lgj.springboot_lgj.utils.Result;
import com.lgj.springboot_lgj.utils.ResultUtil;
import com.lgj.springboot_lgj.web.vm.UserVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@Api(tags = "用户模块")
@RestController
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    private BaseUserExtMapper mapper;
    @Autowired
    private IBaseUserService service;
    //分页查询
    @ApiOperation("通过用户名和角色名分页查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "每页大小",required = true,dataType = "int"),
            @ApiImplicitParam(name = "username",value = "用户名 支持模糊查询",required = true),
            @ApiImplicitParam(name = "role",value = "角色0 管理员   1 普通用户",required = true)
    })
    @GetMapping("/page/query")
    public Result pageQuery(int pageNum, int pageSize, String username, Integer role){
        PageInfo<BaseUser> pageInfo = service.pageQuery(pageNum, pageSize, username, role);
        return ResultUtil.success(pageInfo);
    }
    //更新用户信息
    @ApiOperation("更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true),
            @ApiImplicitParam(name = "telephone",value = "电话",required = true),
            @ApiImplicitParam(name = "realname",value = "真实名字",required = true),
            @ApiImplicitParam(name = "gender",value = "性别0男 1女",required = true,dataType = "int"),
            @ApiImplicitParam(name = "dob",value = "出生日期",required = true),
            @ApiImplicitParam(name = "email",value = "邮箱地址",required = true)
    })
    @PutMapping("/update")
    public Result updateUser(UserVM vm){
        service.updateUser(EntityUtil.vm2Entity(vm,BaseUser.class));
        return ResultUtil.success("更新成功");
    }

    //修改用户头像
    @ApiOperation("修改用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "账号",required = true,dataType = "int"),
            @ApiImplicitParam(name = "userIcon",value = "用户头像",required = true),
    })
    @PutMapping("/updateUserIcon")
    public Result updateUserIcon(Integer id,String userIcon) {
        service.updateUserIcon(id,userIcon);
        return ResultUtil.success("修改用户头像成功");
    }

    //删除用户信息
    @ApiOperation("通过id删除用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "int"),
    })
    @DeleteMapping("/delete")
    public Result deleteUserById(Integer id) {
        service.deleteUserById(id);
        return ResultUtil.success("删除成功");
    }
    //获取当前用户信息
    @ApiOperation("获取当前登录用户的基本信息")
    @GetMapping("/info")
    public Result findUserByToken(HttpServletRequest request) {
       String token = request.getHeader("Authorization");
        BaseUser baseUser = service.currentUserInfo(token);
        return  ResultUtil.success(baseUser);
    }
}
